﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MeetingTopic.aspx.cs" Inherits="Meeting.Web.Admin.MeetingSet.MeetingTopic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>会议议题</title>
    <link href="../../Css/SmartWizard/smart_wizard.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../Scripts/jquery-1.8.2.min.js"></script>
    <!--ardialog-->
    <script type="text/javascript" src="../../Scripts/artDialog4.1.6/artDialog.js?skin=blue"></script>
    <script type="text/javascript" src="../../Scripts/artDialog4.1.6/plugins/iframeTools.js"></script>
    <link href="../../Css/main.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/themes/cupertino/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/themes/icon.css" />
    <script type="text/javascript" src="../../Scripts/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../Scripts/locale/easyui-lang-zh_CN.js"></script>
    <script src="../../Scripts/datagrid-detailview.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../Scripts/common.js"></script>
    <script src="../../Scripts/Json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        var meetingId = request("Id");
        var CategoryId = request("CategoryId");
        var arr = new Array(); //用来存放页面加载时存放的议题
        var delArr = new Array(); //从最初议题中移除的议题
        var allDelArr = new Array(); //存储移除的议题
        var newAddArr = new Array(); //用于存放新添加的议题
        $(function () {
            $("#MeetingName").html(unescape(request("Name")));
            $('#topicSelect').datagrid({
                url: 'MeetingOperation.ashx?method=GetSelectTopic&Id=' + meetingId, //读取已经选择的议题，并以JSON对象的形式返回来
                singleSelect: true,
                onLoadSuccess: function (data) {
                    //用来保存界面初始化时显示的所有的议题
                    if (arr.length == 0) {
                        for (i = 0; i < data.total; i++) {
                            arr.push(data.rows[i].TopicId);
                        }
                    }
                }
            });
        });
        //选择议题
        function SelectTopic() {
            //获取已经选择的议题
            var allArr = new Array();  //用来存放当前已经选择的议题
            var data = $('#topicSelect').datagrid('getData');
            for (i = 0; i < data.total; i++) {
                allArr.push(data.rows[i].TopicId);
            }
            // alert(allArr.length);
            // alert(delArr.length);

            if (CategoryId == "" || CategoryId == undefined) {
                $.messager.alert("提示", "请选择会议类型");
                return;
            }

            var radStr = Math.random();
            var url = "../SelectBase/SelectTopic.aspx" + '?Radstr=' + radStr + "&CategoryId=" + CategoryId;
            art.dialog.open(url, {
                id: 'AddTopic', title: '选择议题', width: 650, height: 520, lock: true,
                close: function () {
                    //以Json对象的结构放回选择议题界面所选择的议题
                    var selNodeJson = art.dialog.data('selJson'); // 读取人员选择返回值
                    //  alert(selNodeJson);
                    if (selNodeJson != "") {
                        try {
                            var data = JSON.parse(selNodeJson);

                            //    DelSelectTopic();//此方法用于清除原先（或移除后）的议题

                            //对对新添加的议题进行筛选，筛选的规则是以前存在的议题不能再出现
                            //将现存的议题与已经删除的议题进行结合（即组成一个数组）
                            //for (i = 0; i < allDelArr.length; i++) {
                            //    allArr.push(allDelArr[i]);
                            //}
                            for (j = 0; j < allArr.length; j++) {
                                for (i = 0; i < data.length; i++) {
                                    if (data[i].TopicId == allArr[j]) {
                                        data.splice(i, 1);
                                    }
                                }
                            }
                            for (i = 0; i < data.length; i++) {
                                //将符合要求的数组添加到newAddArr数组中去
                                newAddArr.push(data[i].TopicId);
                                $('#topicSelect').datagrid('appendRow', data[i]);
                            }
                        } catch (ex) { }
                    }
                }
            }, true);

        }
        function DelSelectTopic() {
            var rows = $('#topicSelect').datagrid('getRows');
            for (i = rows.length - 1; i >= 0; i--) {
                $('#topicSelect').datagrid('deleteRow', i);
            }
        }
        function EditTopicSort() {
            var rows = $('#topicSelect').datagrid('getRows');
            for (i = 0; i < rows.length; i++) {
                $('#topicSelect').datagrid('beginEdit', i);
            }
        }


        //从已选择中移除
        function DelSelect(val, row) {
            return "<a href='#' class='easyui-linkbutton' onclick=\"RemoveSelect('" + row.TopicId + "');\"  >移除</a> ";
        }
        function RemoveSelect(TopicId) {
            //将要移除的议题保存起来
            for (i = 0; i < arr.length; i++) {
                if (arr[i] == TopicId) {
                    delArr.push(TopicId);
                }
            }
            allDelArr.push(TopicId);
            var data = $('#topicSelect').datagrid('getData');
            var index = -1;
            for (i = 0; i < data.total; i++) {
                if (data.rows[i].TopicId == TopicId) {
                    index = i;
                    break;
                }
            }
            //如果新添加的议题被移除需要进行下面的处理
            for (i = 0; i < newAddArr.length; i++) {
                if (newAddArr[i] == TopicId) {
                    newAddArr.splice(i, 1);
                }
            }
            $('#topicSelect').datagrid('deleteRow', index);
        }
        //设置结束编辑
        function GetSelectTopic() {
            var rows = $('#topicSelect').datagrid('getRows');
            for (i = 0; i < rows.length; i++) {
                var ed = $('#topicSelect').datagrid('getEditor', { index: i, field: 'Sort' });

                if (ed != null && $(ed.target).val() == "") $(ed.target).val("10");
                $('#topicSelect').datagrid('endEdit', i);
            }
        }
        function formatTopicEmployee(val, row) {
            return "<a href='#' class='easyui-linkbutton' onclick=\"TopicEmployee('" + row.TopicId + "');\"  >设置主讲人</a> ";
        }
        function TopicEmployee(id) {
            var radStr = Math.random();
            var url = "../SelectBase/SelectMeetingUser.aspx" + '?Radstr=' + radStr + "&MeetingId=" + meetingId;
            art.dialog.open(url, {
                id: 'ChangeTopicEmploye', title: '设置主讲人', width: 500, height: 350, lock: true,
                close: function () {
                    //以Json对象的结构放回选择议题界面所选择的议题
                    var selNodeJson = art.dialog.data('selJson'); // 读取人员选择返回值
                    //  alert(selNodeJson);
                    if (selNodeJson != "") {
                        try {
                            var data = JSON.parse(selNodeJson);
                            var rows = $("#topicSelect").datagrid("getRows");
                            for (i = 0; i < rows.length; i++) {
                                if (rows[i].TopicId == id) {
                                    var row = rows[i];
                                    row.EmployeeId = data[0].EmployeeId;
                                    row.EmployeeName = data[0].EmployeeName;
                                    row.EmployeeTitle = data[0].EmployeeTitle;
                                    row.RoleId = data[0].RoleId;
                                    $("#topicSelect").datagrid("updateRow", { index: i, row: row });
                                    break;
                                }
                            }
                        } catch (ex) { }
                    }
                }
            }, true);

        }

        //保存会议议题        
        function SaveMeetingTopic() {
            //  alert("删除的议题：" + delArr);
            // alert("新添加的议题：" + newAddArr);
            //
            GetSelectTopic(); //结束编辑
            var data = $('#topicSelect').datagrid('getData');
            var str = JSON.stringify(data.rows);
            var Id = meetingId;
            if (Id == "" || Id == undefined) {
                $.messager.alert("提示", "没有找到会议Id");
                return;
            }
            var postData = { Id: Id, data: str };
            //提交

            $.post("MeetingOperation.ashx?method=SaveMeetingTopic", postData, function (data) {
                if (data.success == true) {
                    $.messager.alert("提示", "保存成功");
                    //保存成功，退出当前对话框
                    art.dialog.close();
                } else {
                    $("#error").val(data.Message); $.messager.alert("提示", data.Message);
                }
            });
        }
    </script>
    <style type="text/css">
        input, textarea
        {
            border: 1px solid #AED0EA;
            padding: 2px;
            height: 20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="showView1" cellpadding="0" cellspacing="0" align="center">
            <tr>
                <td class="title">
                    会议名称:
                </td>
                <td class="td_Modify1">
                    <span id="MeetingName"></span>
                </td>
            </tr>
            <tr>
                <td class="title">
                    关联议题:
                </td>
                <td class="td_Modify1">
                    <a id="A1" class="easyui-linkbutton" onclick="SelectTopic();">添加议题</a> <a id="A2"
                        class="easyui-linkbutton" onclick="EditTopicSort();">编辑</a>
                    <table id="topicSelect" style="width: 540px; height: 300px;" data-options="nowrap:false,fitColumns:true,singleSelect:true">
                        <thead>
                            <tr>
                                <th data-options="field:'TopicId',width:80,hidden:true">
                                    编号
                                </th>
                                <th data-options="field:'Title',width:200">
                                    标题
                                </th>
                                <th data-options="field:'EmployeeId',width:200,hidden:true">
                                    人员编号
                                </th>
                                <th data-options="field:'EmployeeTitle',width:80,hidden:true">
                                    职务
                                </th>
                                <th data-options="field:'EmployeeName',width:70 ,editor:'text'">
                                    主讲人
                                </th>
                                <th data-options="field:'RoleId',width:80,hidden:true">
                                </th>
                                <th data-options="field:'Sort',width:40,editor:'text'">
                                    排序
                                </th>
                                <th data-options="field:'action',width:80,formatter:formatTopicEmployee">
                                </th>
                                <th data-options="field:'action1',width:80,formatter:DelSelect">
                                </th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="title">
                </td>
                <td class="td_Modify1">
                    <a href="#" class="easyui-linkbutton" onclick="SaveMeetingTopic();">保存</a>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
